var model = require('./model.js');
var fs =  require('fs');
var path = require('path');
var url = require('url');
var checkLogin = require('./checkLogin.js');
var cookieParse = require('./cookieParse.js');

module.exports.list = function(req, res) {
    if(checkLogin(req)) {
        model.getAll(function(result) {
            var viewPath = path.join('./public', 'list.html');
            var viewData = { employees: result, admin: req.session};
            res.render(viewPath, viewData);
        });
    }else{
        res.redirect('/');
    }
}
module.exports.huifu = function(req, res) {
    
    if(checkLogin(req)) {
        model.getAll(function(result) {
            var viewPath = path.join('./public', 'huifu.html');
            var viewData = { employees: result, employee:result, admin: req.session};
            res.render(viewPath, viewData);
        });

    }else{
        res.redirect('/');
    }
}
module.exports.xingming = function (req, res) {
    var id = req.gcp().id;

    model.getByX('id', id, function(result) {
        var viewPath = path.join('./public', 'xingming.html');
        var viewData = { employees: result,employee: result };
        res.render(viewPath, viewData);
    });
}
module.exports.info = function(req, res) {
    if(checkLogin(req)) {
        model.getAl(function(result) {
            var viewPath = path.join('./public', 'info.html');
            var viewData = { admin: req.session,admin_copy:result,admin_copys:result };
            res.render(viewPath, viewData);
        });
    }else{
        res.redirect('/');
    }
}

module.exports.found = function(req, res) {
    if(checkLogin(req)) {
        model.getA(function(result) {
            var viewPath = path.join('./public', 'found.html');
            var viewData = { admin: req.session,admin:result,admins:result };
            res.render(viewPath, viewData);
        });
    }else{
        res.redirect('/');
    }
}
module.exports.search = function (req, res) {
    var searchBy = req.gcp().searchBy;
    var keywords = req.gcp().keywords;
    var filedName = searchBy;
    var value = keywords;

    model.getByField(filedName, value, function(result) {
        var viewPath = path.join('./public', 'list.html');
        var viewData = { employees: result, admin: result };
        res.render(viewPath, viewData);
    });
}
module.exports.sousuo = function (req, res) {
    var sousuoBy = req.gcp().sousuoBy;
    var keywords = req.gcp().keywords;
    var filedName = sousuoBy;
    var value = keywords;

    model.getBySou(filedName, value, function(result) {
        var viewPath = path.join('./public', 'hui.html');
        var viewData = { replys: result, admin: result };
        res.render(viewPath, viewData);
    });
}
module.exports.sou = function (req, res) {
    var souBy = req.gcp().souBy;
    var keywords = req.gcp().keywords;
    var filedName = souBy;
    var value = keywords;

    model.getByTi(filedName, value, function(result) {
        var viewPath = path.join('./public', 'mytizi.html');
        var viewData = { employees: result, admin: result };
        res.render(viewPath, viewData);
    });
}
module.exports.find = function (req, res) {
    var findBy = req.gcp().findBy;
    var keywords = req.gcp().keywords;
    var filedName = findBy;
    var value = keywords;

    model.getByN(filedName, value, function(result) {
        var viewPath = path.join('./public', 'found.html');
        var viewData = { admins: result, admin: result };
        res.render(viewPath, viewData);
    });
}
module.exports.enroll = function (req, res) {
    var viewPath = path.join('./public', 'enroll.html');
    var viewData = {};
    res.render(viewPath, viewData);
}
module.exports.charu = function (req, res) {
    req.pcp(function(postData) {
        var admin = {
            username : postData.username,
            password : postData.password,
            numble : postData.numble,
            mail : postData.mail,
            sex : postData.sex,
        };
        model.charu(admin, function(data) {
            res.redirect('/login');
        });
    });
}

module.exports.charu = function (req, res) {
    req.pcp(function(postData) {
        var admin = {
            username : postData.username,
            password : postData.password,
            numble : postData.numble,
            mail : postData.mail,
            sex : postData.sex,
        };
        model.charu(admin, function(data) {
            res.redirect('/login');
        });
    });
}
module.exports.tianjia = function (req, res) {
    req.pcp(function(postData) {
        var reply = {
            user : postData.user,
            attach : postData.attach,
            comments : postData.comments,
        };
        model.tianjia(reply, function(data) {
            res.redirect('/list');
        });
    });
}
module.exports.add = function(req, res) {
    if(checkLogin(req)) {
        model.getAll(function(result) {
            var viewPath = path.join('./public', 'add.html');
            var viewData = { employees: result, admin: req.session };
            res.render(viewPath, viewData);
        });
    }else{
        res.redirect('/');
    }
}

/*module.exports.add = function (req, res) {
    var viewPath = path.join('./public', 'add.html');
    var viewData = {};
    res.render(viewPath, viewData);
}*/

module.exports.insert = function (req, res) {
    req.pcp(function(postData) {
        var employee = {
            name : postData.name,
            gender : postData.gender,
            birthdate : postData.birthdate,
            department : postData.department,
            remarks : postData.remarks
        };
        model.insert(employee, function(data) {
            res.redirect('/list');
        });
    });
}
module.exports.plug = function (req, res) {
    req.pcp(function(postData) {
        var admin_copy = {
            username : postData.username,
            sex : postData.sex,
        };
        model.plug(admin_copy, function(data) {
            res.redirect('/info');
        });
    });
}
/*module.exports.dele = function (req, res) {
    var id = req.gcp().id;

    model.delById(id, function () {
        res.redirect('/info');
    })
}*/
module.exports.delete = function (req, res) {
    var id = req.gcp().id;

    model.deleById(id, function () {
        res.redirect('/list');
    })
}
module.exports.throw = function (req, res) {
    var id = req.gcp().id;

    model.throwById(id, function () {
        res.redirect('/info');
    })
}
module.exports.modify = function (req, res) {
    var id = req.gcp().id;

    model.getByField('id', id, function(result) {
        var viewPath = path.join('./public', 'modify.html');
        var viewData = { employees: result,employee: result };
        res.render(viewPath, viewData);
    });
}

module.exports.concern = function (req, res) {
    var id = req.gcp().id;

    model.getByF('id', id, function(result) {
        var viewPath = path.join('./public', 'concern.html');
        var viewData = { admins: result,admin: result };
        res.render(viewPath, viewData);
    });
}


/*module.exports.huifu = function (req, res) {
    if(checkLogin(req)) {
    
}*/
module.exports.update = function(req, res) {
    var id = req.gcp().id;

    req.pcp(function (postData){
        var employee = {
            id: id,
            name : postData.name,
            gender : postData.gender,
            birthdate : postData.birthdate,
            department : postData.department,
            remarks : postData.remarks
        };
        model.updateById(employee, function() {
            res.redirect('/list');
        });
    });
  };
  
module.exports.home = function(req, res){
    var viewPath = path.join('./public', 'login.html');
    var viewData = {}; 
    res.render(viewPath, viewData);
}

module.exports.login = function (req, res) {
    req.pcp(function (postData) {
        var username = postData.username;
        var password = postData.pwd;

        model.checkAdmin(username, password, function (result){
            if (result.length == 0) {
                res.redirect('/');
            }else{
                var sessionId = Date.now() + '_' + Math.random();
                res.setHeader('Set-Cookie', 'sessionId=' + sessionId);
                session[sessionId] = {
                    id : result[0].id,
                    username: result[0].username
                }
                res.redirect('/list');
            }
        })
    });
}

module.exports.logout = function(req, res) {
    var cookie = cookieParse(req);
    delete session[cookie.sessionId];
    res.redirect('/');
}
